package com.example.speedwaymanager;

import java.util.ArrayList;
import java.util.Iterator;

import org.w3c.dom.Document;

import android.app.Activity;
import android.util.Log;

public class CGameManager {

	private static CGameManager _instance = new CGameManager();
	private static ArrayList<CRider> _ridersList = new ArrayList<CRider>();
	private static ArrayList<String> _teamStrList = new ArrayList<String>();
	private static ArrayList<CTeam> _teamsList = new ArrayList<CTeam>();
	private static CLeague eLeague = new CLeague("ELeague");
	private CSchedule _schedule;
	private ArrayList<String> _matchResults = new ArrayList<String>();
	private CMatch _match;
	private Document _xmlDoc;
	private String _xml;
	private String _xmlTeams;
	private Document _xmlDocTeams;
	private ArrayList<CTeam> _selectedTeams= new ArrayList<CTeam>();
	private ArrayList<String> _selectedTeamsStr= new ArrayList<String>();
	private boolean _homeTeamChecked = true;
	private boolean _awayTeamChecked = false;
	private CTeam _selectedSquadTeam;
	private Activity _mainActivity;
	
	/**
	 * private constructor to prevent creating new class object (Singleton)
	 */
	private CGameManager() {		
	}
	
	/**
	 * public access point to CGameManager instance
	 * @return
	 */	
	public static synchronized CGameManager getInstance() {
		return _instance;
	}
	/**
	 * Initialize database. Read from XML
	 * 
	 * @param aMain
	 *            Main activity needed for asset
	 */
	public void loadDataBase(Activity main)
	{
		// * load database (riders, teams, avgs) from xml file located in assets
		_mainActivity = main;
		CUtils.loadDatabaseFromXml(main.getAssets(), _ridersList);
		CUtils.loadTeamsFromXml(main.getAssets(), _teamStrList);		
		addTeamsAndRiders();
	}
	
	public void startSingleMatch(ArrayList<CTeam> team) {
		_schedule = new CSchedule();
		CUtils.loadScheduleFromXml(_mainActivity.getAssets(), _schedule);
		_match = new CMatch(team, _schedule);
		_matchResults.clear();
		/*
		 	for (int i = 0; i < _match.get_heats().size(); ++i) {
		   	_match.analyzeChangeRequest(i);
		   	_match.startNextHeat();
			ArrayList<String> results = getHeatResults(i);
			for (String str : results) {
				_matchResults.add(str);
			}
			Iterator<String> it = results.iterator();
			while (it.hasNext()) {
				Log.d("heat " + i, it.next().toString());
			}
			if (i == 12)
				_match.nominateToHeats14and15();
		}
		*/
	}

	public void startNextHeat() {
		_match.startNextHeat();
	}

	/**
	 * logs from heats
	 * 
	 * @param heat
	 *            heat number
	 * @return array of strings for current heat
	 */
	public ArrayList<String> getHeatResults(int heat) {
		Iterator<CRider> itRider = _match.getHeatResults(heat).iterator();
		ArrayList<String> riderNames = new ArrayList<String>();
		while (itRider.hasNext()) {
			CRider rider = itRider.next();
			riderNames.add(new String(rider.get_matchStats().get_number() + "."
					+ rider.get_name() + " "				
					+ rider.get_matchStats().get_points() + "+"
					+ rider.get_matchStats().get_bonuses() + " "
					+ rider.get_matchStats().getStringPoints() ));
		}

		riderNames.add(_match.get_teams().get(0).get_name() + " "
				+ _match.get_teams().get(0).get_points() + " - "
				+ _match.get_teams().get(1).get_name() + " "
				+ _match.get_teams().get(1).get_points());
		return riderNames;
	}

	public int getCurrentHeatNo() {
		return _match.getCurrentHeat();
	}
	
	public CHeat getCurrentHeat() {
		return _match.get_heats().get(getCurrentHeatNo());
	}

	public int getLastHeat() {
		return _match.get_LastHeat();
	}

	/**
	 * add riders to proper teams
	 */
	private static void addTeamsAndRiders() {
		Iterator<CRider> itRider = _ridersList.iterator();
		Iterator<CTeam> itTeam = _teamsList.iterator();
		Iterator<String> itStr = _teamStrList.iterator();

		// add team with names
		while (itStr.hasNext()) {
			String strTeam = itStr.next();
			CTeam team = new CTeam(strTeam);
			_teamsList.add(team);
			eLeague.addTeam(team);
		}

		// add riders to teams
		while (itRider.hasNext()) {
			CRider rider = itRider.next();
			itTeam = _teamsList.iterator();
			while (itTeam.hasNext()) {
				CTeam team = itTeam.next();
				if (team.get_name().equals(rider.get_team())) {
					team.addRiderToTeam(rider);
					break;
				}
			}
		}
	}

	public CTeam getTeamByName(String name) {
		for (CTeam team : _teamsList) {
			if (team.get_name().equals(name)) {
				return team;
			}
		}
		return null;
	}

	/**
	 * @return the ridersList
	 */
	public ArrayList<CRider> getRidersList() {
		return _ridersList;
	}

	/**
	 * @return the _xmlDoc
	 */
	public Document get_xmlDoc() {
		return _xmlDoc;
	}

	/**
	 * @return the _xml
	 */
	public String get_xml() {
		return _xml;
	}

	/**
	 * @return the _xmlTeams
	 */
	public String get_xmlTeams() {
		return _xmlTeams;
	}

	/**
	 * @return the eLeague
	 */
	public CLeague geteLeague() {
		return eLeague;
	}

	/**
	 * @return the _match
	 */
	public CMatch get_match() {
		return _match;
	}

	/**
	 * @param _match
	 *            the _match to set
	 */
	public void set_match(CMatch _match) {
		this._match = _match;
	}

	/**
	 * @return the _teamStrList
	 */
	public ArrayList<String> get_teamStrList() {
		return _teamStrList;
	}

	/**
	 * @return the _teamsList
	 */
	public ArrayList<CTeam> get_teamsList() {
		return _teamsList;
	}

	/**
	 * @return the _matchResults
	 */
	public ArrayList<String> get_matchResults() {
		return _matchResults;
	}

	/**
	 * @return the _selectedTeams
	 */
	public ArrayList<CTeam> get_selectedTeams() {
		return _selectedTeams;
	}

	/**
	 * @param _selectedTeams the _selectedTeams to set
	 */
	public void set_selectedTeams(ArrayList<CTeam> _selectedTeams) {
		this._selectedTeams = _selectedTeams;
	}

	/**
	 * @return the _homeTeamChecked
	 */
	public boolean is_homeTeamChecked() {
		return _homeTeamChecked;
	}

	/**
	 * @param _homeTeamChecked the _homeTeamChecked to set
	 */
	public void set_homeTeamChecked(boolean _homeTeamChecked) {
		this._homeTeamChecked = _homeTeamChecked;
	}

	/**
	 * @return the _awayTeamChecked
	 */
	public boolean is_awayTeamChecked() {
		return _awayTeamChecked;
	}

	/**
	 * @param _awayTeamChecked the _awayTeamChecked to set
	 */
	public void set_awayTeamChecked(boolean _awayTeamChecked) {
		this._awayTeamChecked = _awayTeamChecked;
	}

	/**
	 * @return the _selectedTeamsStr
	 */
	public ArrayList<String> get_selectedTeamsStr() {
		return _selectedTeamsStr;
	}

	/**
	 * @param _selectedTeamsStr the _selectedTeamsStr to set
	 */
	public void set_selectedTeamsStr(ArrayList<String> _selectedTeamsStr) {
		this._selectedTeamsStr = _selectedTeamsStr;
	}

	/**
	 * @return the _selectedSquadTeam
	 */
	public CTeam get_selectedSquadTeam() {
		return _selectedSquadTeam;
	}

	/**
	 * @param _selectedSquadTeam the _selectedSquadTeam to set
	 */
	public void set_selectedSquadTeam(CTeam _selectedSquadTeam) {
		this._selectedSquadTeam = _selectedSquadTeam;
	}
}
